#!/bin/sh

### BEGIN ###
# Author: idevz
# Since: 2016/04/09
# Description:       Setup Vanilla DemoApp named vademo
### END ###

VANILLA_VERSION=0.1.0.rc7
VANILLA_VERSION_DIR_STR=0_1_0_rc7
VANILLA_APP_ROOT=/data/vanilla/vademo
VANILLA_APP_USER=idevz
VANILLA_APP_GROUP=sina
VANILLA_RUNNING_ENV='' #default '' is for production environment
TIME_MARK=`date "+%Y_%m_%d_%H_%M_%S"`
PLATFORM=`uname`
ECHO_E=" -e "
[ $PLATFORM = "Darwin" ] && ECHO_E=""


alert()
{
    MSG=$1
    echo $ECHO_E"\033[31m$MSG \033[0m\n"
}

init_vanilla_app()
{
    VANILLA_APP_ROOT=$1
    echo $ECHO_E"\033[40mBegin init Vanilla App: $VANILLA_APP_ROOT \033[0m\n"
    [ -n $2 ] && VANILLA_APP_USER=$2
    [ -n $3 ] && VANILLA_APP_GROUP=$3
    [ -n $4 ] && APP_NAME=$4
    [ -n $5 ] && VANILLA_RUNNING_ENV=$5

    SERVICE_SH=$VANILLA_APP_ROOT/va-$APP_NAME-service
    VANILLA_BIN=/usr/local/bin/vanilla-$VANILLA_VERSION
    [ -d $VANILLA_APP_ROOT ] && mv -f $VANILLA_APP_ROOT $VANILLA_APP_ROOT".old_"$TIME_MARK
    [ -x $VANILLA_BIN ] && $VANILLA_BIN new $VANILLA_APP_ROOT
    if [[ $? -ne 0 ]];then
        alert $VANILLA_BIN" is not exist, You shoud install vanilla first, to see './setup-framework -h' for more info."
        exit 1
    fi
    sed "s/#user zhoujing staff/user $VANILLA_APP_USER $VANILLA_APP_GROUP/" $VANILLA_APP_ROOT/nginx_conf/va-nginx-development.conf>$VANILLA_APP_ROOT/nginx_conf/va-nginx-development.conf.tmp
    sed "s/#user zhoujing staff/user $VANILLA_APP_USER $VANILLA_APP_GROUP/" $VANILLA_APP_ROOT/nginx_conf/va-nginx.conf>$VANILLA_APP_ROOT/nginx_conf/va-nginx.conf.tmp
    mv -f $VANILLA_APP_ROOT/nginx_conf/va-nginx-development.conf.tmp $VANILLA_APP_ROOT/nginx_conf/va-nginx-development.conf
    mv -f $VANILLA_APP_ROOT/nginx_conf/va-nginx.conf.tmp $VANILLA_APP_ROOT/nginx_conf/va-nginx.conf
    chmod +x $SERVICE_SH
    sudo $SERVICE_SH initconf $VANILLA_RUNNING_ENV
    sudo $SERVICE_SH start $VANILLA_RUNNING_ENV
    sudo $SERVICE_SH restart $VANILLA_RUNNING_ENV
    echo $ECHO_E"\033[40mVanilla App: $APP_NAME is running \033[0m\n"
}

show_usage()
{
    echo $ECHO_E"`printf %-16s "Usage: $0"` -h   show this help info"
    echo $ECHO_E"`printf %-16s ` -a   VANILLA_APP_ROOT, app absolute path(which path to init app)"
    echo $ECHO_E"`printf %-16s ` -u   VANILLA_APP_USER, user to run app"
    echo $ECHO_E"`printf %-16s ` -g   VANILLA_APP_GROUP, user group to run app"
    echo $ECHO_E"`printf %-16s ` -e   VANILLA_RUNNING_ENV, app running environment"
}

while getopts a:u:g:e:h OPT; do
    case "$OPT" in
        a )
            VANILLA_APP_ROOT=$OPTARG
            ;;
        u )
            VANILLA_APP_USER=$OPTARG
            ;;
        g )
            VANILLA_APP_GROUP=$OPTARG
            ;;
        e )
            VANILLA_RUNNING_ENV=$OPTARG
            ;;
        h )
            show_usage && exit 0
            ;;
        -- )
            shift break
            ;;
        ? )
            alert "ERROR: unknown argument!" && show_usage && exit 1
            ;;
    esac
done

APP_NAME=`basename $VANILLA_APP_ROOT`
init_vanilla_app $VANILLA_APP_ROOT $VANILLA_APP_USER $VANILLA_APP_GROUP $APP_NAME $VANILLA_RUNNING_ENV

echo
echo $ECHO_E"You are setup $APP_NAME and using:"
echo $ECHO_E"`printf %-36s "\"$VANILLA_APP_ROOT\""` ---- as vanilla app root"
echo $ECHO_E"`printf %-36s "\"$VANILLA_RUNNING_ENV\""` ---- as running environment"
echo $ECHO_E"`printf %-36s "\"$VANILLA_APP_USER\""` ---- as vanilla app nginx user"
echo $ECHO_E"`printf %-36s "\"$VANILLA_APP_GROUP\""` ---- as vanilla app nginx group"
echo

exit 0